package com.aispeech.detector.utils;

public class DataUtils {

    public static short byteArrayToShort(byte[] b, int offset) {
        return (short) ((b[offset] & 0xFF) | (b[offset + 1] & 0xFF) << 8);
    }

    public static short countAmplitude(byte[] buffer, int bufferLen) {
        int channels = 1;
        int frameCount = bufferLen / channels / 2;
        int frameSize = channels * 2;
        int frame, offset;
        short amplitue;
        short maxAmplitudeChannel = 0;

        for (offset = 0, frame = 0; frame < frameCount; frame++) {
            amplitue = (short) Math.abs(byteArrayToShort(buffer, offset));
            if (amplitue > maxAmplitudeChannel) {
                maxAmplitudeChannel = amplitue;
            }
            offset = frame * frameSize;
        }
        return maxAmplitudeChannel;
    }

}
